package com.csyf.manager.controller;

import com.csyf.manager.entity.*;
import com.csyf.manager.service.SysAgentService;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.service.SysMerchBiz;
import com.csyf.manager.service.SysTradeTypeBiz;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: casper
 * @Description: 后台链接入口
 * @Date: Created in 2018/11/22 15:16
 */
@Controller
public class RenderController {

    @Autowired
    private SysAgentService sysAgentService;

    @Autowired
    private SysMerchBiz sysMerchBiz;

    @Autowired
    private SysTradeTypeBiz sysTradeTypeBiz;

    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;

    /**工作台**/
    @GetMapping("/workdest")
    public String workdest(){
        return "index/workdest";
    }

    /**用户列表入口**/
    @GetMapping("/users")
    public String userList(){
        return "user/list";
    }

    /**角色列表入口**/
    @GetMapping("/roles")
    public String roleList() {
        return "role/list";
    }

    /**菜单列表入口**/
    @GetMapping("menus")
    public String menuList() {
        return "menu/list";
    }

    /**代理商列表入口**/
    @GetMapping("agents")
    public String agentsList() {
        return "agent/list";
    }

    /**商户列表入口**/
    @GetMapping("merchs")
    public String merchsList() {
        return "merch/list";
    }

    /**代理商交易类型**/
    @GetMapping("tradeTypes")
    public String tradeTypeList() {
        return "tradeType/list";
    }

    /**订单列表入口**/
    @GetMapping("orders")
    public String orderList(Model model) {
        List<SysChannelAttr> tradeType = sysChannelAttrBiz.findTradeType();
        model.addAttribute("tradeList", tradeType);
        return "order/list";
    }

    /**代付列表入口**/
    @GetMapping("payments")
    public String paymentList(Model model) {
        List<SysChannelAttr> tradeType = sysChannelAttrBiz.findDfTradeType();
        model.addAttribute("tradeList", tradeType);
        return "payment/list";
    }

    /**当日交易统计入口**/
    @GetMapping("dailyTradeSum")
    public String dailyTradeSumList() {
        return "report/dailyTradeSumList";
    }

    /**收益统计入口**/
    @GetMapping("profitSum")
    public String profitSumList() {
        return "report/profitSumList";
    }

    /**对账列表入口**/
    @GetMapping("billReport")
    public String billReportList() {
        return "report/billReportList";
    }

    /**余额汇总入口**/
    @GetMapping("balanceSummary")
    public String balanceSummaryList() {
        return "report/balanceSummaryList";
    }

    /**产品列表入口**/
    @GetMapping("products")
    public String productList() {
        return "product/list";
    }

    /**
     * 产品配置入口
     * @return
     */
    @GetMapping("productAttrs")
    public String productAttrList() {
        return "channelAttr/list";
    }

    /** 交易通道列表入口 **/
    @GetMapping("/tradeChans")
    public String tradeChanList() {
        return "channel/tradeChanList";
    }

    /** 代付通道列表入口 **/
    @GetMapping("/paymentChans")
    public String paymentChanList() {
        return "channel/paymentChanList";
    }


    /******************************************************商户链接入口************************************************/

    /**商户信息入口**/
//    @GetMapping("merchInfo")
//    public String merchInfo() {
//        return "merchant/merchInfo";
//    }

    /**商户余额入口**/
//    @GetMapping("merchAccBalance")
//    public String merchAccBalance() {
//        return "merchant/merAccBalance";
//    }

    /**商户订单列表入口**/
    @GetMapping("merchOrders")
    public String merchOrderList(Model model) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysMerchant merchant = sysMerchBiz.queryAgentByUserId(sysUser.getUserId());
        List<SysTradeType> tradeTypeByMchId = sysTradeTypeBiz.findTradeTypeByMchId(merchant.getMerchMerchantNo());
        List<SysTradeType> sysTradeTypes = new ArrayList<>();
        for (SysTradeType entry : tradeTypeByMchId) {
            if (!entry.getTradeType().contains("DF")) {
                sysTradeTypes.add(entry);
            }
        }
        model.addAttribute("tradeList", sysTradeTypes);

        return "merchant/merchOrderList";
    }

    /**商户代付列表入口**/
    @GetMapping("merchPayments")
    public String merchPaymentList(Model model) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysMerchant merchant = sysMerchBiz.queryAgentByUserId(sysUser.getUserId());

        List<SysTradeType> tradeTypeByMchId = sysTradeTypeBiz.findTradeTypeByMchId(merchant.getMerchMerchantNo());
        List<SysTradeType> sysTradeTypes = new ArrayList<>();
        for (SysTradeType entry : tradeTypeByMchId) {
            if (entry.getTradeType().contains("DF")) {
                sysTradeTypes.add(entry);
            }
        }
        model.addAttribute("tradeList", sysTradeTypes);

        return "merchant/merchPaymentList";
    }


    /*************************************************代理商链接入口******************************************************/


    /**代理商订单列表入口**/
    @GetMapping("agentOrders")
    public String agentOrderList(Model model) {
        //获取登录的代理商信息
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysAgent agent = sysAgentService.queryAgentByUserId(sysUser.getUserId());

        //获取代理商下的商户信息
        Set<Integer> merchIds = sysMerchBiz.findMerchByAgentId(agent.getId());
        List<SysMerchant> merchants = new ArrayList<>();
        for (Integer mchId : merchIds) {
            SysMerchant sysMerchant = sysMerchBiz.queryAgentById(Long.valueOf(mchId));
            merchants.add(sysMerchant);
        }

        model.addAttribute("merchants", merchants);
        return "sysAgent/agentOrderList";
    }

    /**代理商代付列表入口**/
    @GetMapping("agentPayments")
    public String agentPaymentList(Model model) {
        //获取登录的代理商信息
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysAgent agent = sysAgentService.queryAgentByUserId(sysUser.getUserId());

        //获取代理商下的商户信息
        Set<Integer> merchIds = sysMerchBiz.findMerchByAgentId(agent.getId());
        List<SysMerchant> merchants = new ArrayList<>();
        for (Integer mchId : merchIds) {
            SysMerchant sysMerchant = sysMerchBiz.queryAgentById(Long.valueOf(mchId));
            merchants.add(sysMerchant);
        }
        model.addAttribute("merchants", merchants);
        return "sysAgent/agentPaymentList";
    }
}
